package com.czl.module_storehouse.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.BaseNumBean;
import com.czl.module_base.utils.NumberUtils;
import com.czl.module_storehouse.R;

import java.util.List;

/**
 * @author xubin
 */
public abstract class BaseNumAdapter<T extends BaseNumBean> extends BaseQuickAdapter<T, BaseViewHolder> {

    protected int maxCount;
    protected int minCount;

    public BaseNumAdapter(int layoutResId) {
        super(layoutResId);
    }

    public BaseNumAdapter(int layoutResId, @Nullable List<T> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, T item) {
        setTextNum(helper, item);
    }

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public int getMinCount() {
        return minCount;
    }

    public void setMinCount(int minCount) {
        this.minCount = minCount;
    }

    /**
     * 最大可输入数量
     *
     * @param item
     * @return
     */
    public abstract int getMaxCount(T item);

    /**
     * 最小数量
     *
     * @param item
     * @return
     */
    public abstract int getMinCount(T item);

    private void setTextNum(BaseViewHolder helper, T item) {
        final T bean = item;
        EditText editText = helper.getView(R.id.tv_quantity);
        if (editText.getTag() != null && editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        if (bean != null) {
            editText.setText(String.valueOf(bean.getCount()));
        }
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int count = 0;
                int maxCount = getMaxCount(bean);
                int minCount = getMinCount(bean);
                if (NumberUtils.isNumber(editable.toString())) {
                    count = Integer.parseInt(editable.toString());
                    if (count > maxCount) {
                        count = maxCount;
                        editText.setText(String.valueOf(count));
                        if (mOnNumChangeListener != null) {
                            mOnNumChangeListener.onChange(count);
                        }
                        return;
                    } else if (count < minCount) {
                        count = minCount;
                        editText.setText(String.valueOf(count));
                        if (mOnNumChangeListener != null) {
                            mOnNumChangeListener.onChange(count);
                        }
                        return;
                    }
                }
                editText.setSelection(editText.getText().length());
                if (bean != null) {
                    bean.setCount(count);
                }
                setAfterTextChanged(helper, bean, count);
                if (mOnNumChangeListener != null) {
                    mOnNumChangeListener.onChange(count);
                }
            }
        };
        editText.addTextChangedListener(textWatcher);
        editText.setTag(textWatcher);
        helper.getView(R.id.text_add).setOnClickListener(v -> {
            //加
            if (bean != null) {
                int count = bean.getCount();
                int maxCount = getMaxCount(bean);
                if (count < maxCount) {
                    count++;
                    editText.setText(String.valueOf(count));
                }
            }
        });
        helper.getView(R.id.text_remove).setOnClickListener(v -> {
            //减
            if (bean != null) {
                int count = bean.getCount();
                if (count > getMinCount(bean)) {
                    count--;
                    editText.setText(String.valueOf(count));
                }
            }
        });
    }

    /**
     * 数量变化后的操作
     *
     * @param helper
     * @param bean
     * @param count
     */
    protected abstract void setAfterTextChanged(BaseViewHolder helper, T bean, int count);

    protected OnNumChangeListener mOnNumChangeListener;

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        mOnNumChangeListener = onNumChangeListener;
    }

    public interface OnNumChangeListener {
        /**
         * 文本值改变
         *
         * @param count
         */
        void onChange(int count);
    }
}
